/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Enum.LoaiNguoiDungEnum;
import java.awt.Component;
import java.beans.PropertyVetoException;
import java.util.Date;
import java.util.logging.Level;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import org.apache.log4j.Logger;
import pojo.CuaHang;
import pojo.LoaiNguoiDung;
import pojo.NguoiDung;
import report.SubReport;

/**
 *
 * @author 0
 */
public class MainGUI extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    private static Logger logger = Logger.getLogger(MainGUI.class);
    public static NguoiDung nguoiDung = new NguoiDung(0, new CuaHang(4), new LoaiNguoiDung(5), null, null, null, null, null, null, null, false, null, null);
    //public static NguoiDung nguoiDung=null;

    public MainGUI() {
        initComponents();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        SetStatusBar();
        //SetMenu();
    }

    private void SetStatusBar() {
        if (nguoiDung != null) {
            lblName.setText(String.format("Username: %s  Loại người dùng: %s", nguoiDung.getUsername(), nguoiDung.getLoaiNguoiDung().getTenLoaiNguoiDung()));
        } else {
            lblName.setText("");
        }
    }

    private void SetMenu() {
        if (nguoiDung == null) {
            mnDangNhap.setVisible(true);
            mnDangXuat.setVisible(false);
            mnThayDoiPassword.setVisible(false);
            mnQuanLyNhanVien.setVisible(false);
        } else {
            mnDangNhap.setVisible(false);
            mnDangXuat.setVisible(true);
            mnThayDoiPassword.setVisible(true);
            int idLoaiNguoiDung = nguoiDung.getLoaiNguoiDung().getId();
            if (idLoaiNguoiDung == LoaiNguoiDungEnum.Admin.getCode()) {
                mnQuanLyNhanVien.setVisible(true);
            } else if (idLoaiNguoiDung == LoaiNguoiDungEnum.NhanVienBanHang.getCode()) {
                mnQuanLyNhanVien.setVisible(false);
            } else if (idLoaiNguoiDung == LoaiNguoiDungEnum.NhanVienNhapHang.getCode()) {
                mnQuanLyNhanVien.setVisible(false);
            } else if (idLoaiNguoiDung == LoaiNguoiDungEnum.NhanVienQuanLy.getCode()) {
                mnQuanLyNhanVien.setVisible(true);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        tabMain = new org.xnap.commons.gui.CloseableTabbedPane();
        stName = new org.jdesktop.swingx.JXStatusBar();
        lblName = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnQuanLyNhanVien = new javax.swing.JMenu();
        mnQuanLyHoaDon = new javax.swing.JMenu();
        mnQuanLyPhieuThu = new javax.swing.JMenu();
        mnQuyDinh = new javax.swing.JMenu();
        mnThayDoiPassword = new javax.swing.JMenu();
        mnDangNhap = new javax.swing.JMenu();
        mnDangXuat = new javax.swing.JMenu();
        mnQuanLyKhachHang = new javax.swing.JMenu();
        mnQuanLyKhachHangGUI = new javax.swing.JMenu();
        mnTimKiemKhachHangGUI = new javax.swing.JMenu();
        mnReport = new javax.swing.JMenu();
        mnThongKeKhachHang = new javax.swing.JMenu();

        jMenuItem1.setText("jMenuItem1");

        jMenu1.setText("jMenu1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quản lý chuỗi cửa hàng bán quần áo");

        stName.setLayout(new java.awt.GridLayout(1, 0));
        stName.add(lblName);

        jMenuBar1.setDoubleBuffered(true);

        mnQuanLyNhanVien.setText("Quản lý nhân viên");
        mnQuanLyNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnQuanLyNhanVienMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnQuanLyNhanVien);

        mnQuanLyHoaDon.setText("Quản lý hoá đơn");
        mnQuanLyHoaDon.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnQuanLyHoaDonMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnQuanLyHoaDon);

        mnQuanLyPhieuThu.setText("Quản lý phiếu thu");
        mnQuanLyPhieuThu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnQuanLyPhieuThuMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnQuanLyPhieuThu);

        mnQuyDinh.setText("Quy định");
        mnQuyDinh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnQuyDinhMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnQuyDinh);

        mnThayDoiPassword.setText("Thay đổi Password");
        mnThayDoiPassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnThayDoiPasswordMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnThayDoiPassword);

        mnDangNhap.setText("Đăng nhập");
        mnDangNhap.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDangNhapMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDangNhap);

        mnDangXuat.setText("Đăng xuất");
        mnDangXuat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDangXuatMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDangXuat);

        mnQuanLyKhachHang.setText("Quản lý khách hàng");

        mnQuanLyKhachHangGUI.setText("Quản lý khách hàng");
        mnQuanLyKhachHangGUI.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnQuanLyKhachHangGUIMouseClicked(evt);
            }
        });
        mnQuanLyKhachHang.add(mnQuanLyKhachHangGUI);

        mnTimKiemKhachHangGUI.setText("Tìm kiếm khách hàng");
        mnTimKiemKhachHangGUI.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnTimKiemKhachHangGUIMouseClicked(evt);
            }
        });
        mnQuanLyKhachHang.add(mnTimKiemKhachHangGUI);

        jMenuBar1.add(mnQuanLyKhachHang);

        mnReport.setText("Report");

        mnThongKeKhachHang.setText("Thống kê khách hàng");
        mnThongKeKhachHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnThongKeKhachHangMouseClicked(evt);
            }
        });
        mnReport.add(mnThongKeKhachHang);

        jMenuBar1.add(mnReport);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(stName, javax.swing.GroupLayout.DEFAULT_SIZE, 958, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabMain, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(stName, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnQuanLyNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnQuanLyNhanVienMouseClicked
        
            // TODO add your handling code here:
        
    }//GEN-LAST:event_mnQuanLyNhanVienMouseClicked

    private void mnDangNhapMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDangNhapMouseClicked
        // TODO add your handling code here:
        DangNhapGUI dangNhapGUI = new DangNhapGUI(this, true);
        final int x = (this.getWidth() - dangNhapGUI.getWidth()) / 2;
        final int y = (this.getHeight() - dangNhapGUI.getHeight()) / 2;
        dangNhapGUI.setLocation(x, y);
        dangNhapGUI.setVisible(true);
        SetStatusBar();
        SetMenu();
    }//GEN-LAST:event_mnDangNhapMouseClicked

    private void mnThayDoiPasswordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnThayDoiPasswordMouseClicked
        // TODO add your handling code here:
        ThayDoiPasswordGUI thayDoiPasswordGUI = new ThayDoiPasswordGUI(this, true);
        final int x = (this.getWidth() - thayDoiPasswordGUI.getWidth()) / 2;
        final int y = (this.getHeight() - thayDoiPasswordGUI.getHeight()) / 2;
        thayDoiPasswordGUI.setLocation(x, y);
        thayDoiPasswordGUI.setVisible(true);
    }//GEN-LAST:event_mnThayDoiPasswordMouseClicked

    private void mnDangXuatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDangXuatMouseClicked
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn đăng xuất không?", "Question", JOptionPane.YES_NO_OPTION) == 0) {
            nguoiDung = null;
            SetStatusBar();
            SetMenu();
        }
    }//GEN-LAST:event_mnDangXuatMouseClicked

    private void mnQuanLyHoaDonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnQuanLyHoaDonMouseClicked
        
            // TODO add your handling code here:
        
    }//GEN-LAST:event_mnQuanLyHoaDonMouseClicked

    private void mnQuanLyPhieuThuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnQuanLyPhieuThuMouseClicked
        // TODO add your handling code here:
       
    }//GEN-LAST:event_mnQuanLyPhieuThuMouseClicked

    private void mnQuyDinhMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnQuyDinhMouseClicked
        // TODO add your handling code here:
        try {
            Component[] components = tabMain.getComponents();
            for (Component component : components) {
                if (component instanceof QuyDinhGUI) {
                    tabMain.setSelectedComponent(component);
                    return;
                }
            }
            tabMain.add("Quy định", new QuyDinhGUI());
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_mnQuyDinhMouseClicked

    private void mnTimKiemKhachHangGUIMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnTimKiemKhachHangGUIMouseClicked
        // TODO add your handling code here:
        try {
            Component[] components = tabMain.getComponents();
            for (Component component : components) {
                if (component instanceof TimKiemKhachHangGUI) {
                    tabMain.setSelectedComponent(component);
                    return;
                }
            }
            tabMain.add("Tìm kiếm khách hàng", new TimKiemKhachHangGUI());
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_mnTimKiemKhachHangGUIMouseClicked

    private void mnQuanLyKhachHangGUIMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnQuanLyKhachHangGUIMouseClicked
        // TODO add your handling code here:
        try {
            Component[] components = tabMain.getComponents();
            for (Component component : components) {
                if (component instanceof DanhSachKhachHangGUI) {
                    tabMain.setSelectedComponent(component);
                    return;
                }
            }
            tabMain.add("Quản lý khách hàng", new DanhSachKhachHangGUI());
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_mnQuanLyKhachHangGUIMouseClicked

    private void mnThongKeKhachHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnThongKeKhachHangMouseClicked
        // TODO add your handling code here:
         try {
            Component[] components = tabMain.getComponents();
            for (Component component : components) {
                if (component instanceof ReportKhachHang) {
                    tabMain.setSelectedComponent(component);
                    return;
                }
            }
            tabMain.add("Thống kê khách hàng", new ReportKhachHang());
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }//GEN-LAST:event_mnThongKeKhachHangMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel lblName;
    private javax.swing.JMenu mnDangNhap;
    private javax.swing.JMenu mnDangXuat;
    private javax.swing.JMenu mnQuanLyHoaDon;
    private javax.swing.JMenu mnQuanLyKhachHang;
    private javax.swing.JMenu mnQuanLyKhachHangGUI;
    private javax.swing.JMenu mnQuanLyNhanVien;
    private javax.swing.JMenu mnQuanLyPhieuThu;
    private javax.swing.JMenu mnQuyDinh;
    private javax.swing.JMenu mnReport;
    private javax.swing.JMenu mnThayDoiPassword;
    private javax.swing.JMenu mnThongKeKhachHang;
    private javax.swing.JMenu mnTimKiemKhachHangGUI;
    private org.jdesktop.swingx.JXStatusBar stName;
    private org.xnap.commons.gui.CloseableTabbedPane tabMain;
    // End of variables declaration//GEN-END:variables
}
